package yt.kpool.fragment;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONStringer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.MainActivity;
import yt.kpool.R;
import yt.kpool.activity.miner.AddMinerRemarkActivity;
import yt.kpool.activity.web.ProfitWebAc;
import yt.kpool.adapter.MinerListAdapter;
import yt.kpool.adapter.PersonSelectListviewAdapter;
import yt.kpool.base.BaseApplication;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.ChildAccountBean;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.MinerBean;
import yt.kpool.bean.MinerGroupBean;
import yt.kpool.interfac.Observer;
import yt.kpool.interfac.OnSelectPersonListener;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.DensityUtil;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.SPUtil;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ContentDialog;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.lazyviewpager.LazyFragmentPagerAdapter;
import yt.kpool.view.pop.CurrencyPopupWindow;
import yt.kpool.view.pop.MinerGroupPop;
import yt.kpool.view.pop.MinerSelectPop;
import yt.kpool.view.pop.MoveMinerGroupPop;

/**
 * 矿工
 *
 * @author ygy
 * @日期 2019/10/8 11:17
 * @描述 MinerFragment
 */
public class MinerFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable, Observer {

    @BindView(R.id.recyleview)
    LRecyclerView mRecyclerView;
    @BindView(R.id.edit_content)
    EditText edit_content;//搜索框
    @BindView(R.id.radioButton1)
    RadioButton radioButton1;
    @BindView(R.id.radioButton2)
    RadioButton radioButton2;
    @BindView(R.id.radioButton3)
    RadioButton radioButton3;
    @BindView(R.id.radioButton4)
    RadioButton radioButton4;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.ll_currency)
    LinearLayout ll_currency;
    @BindView(R.id.view_line)
    View view_line;

    @BindView(R.id.tv_sort)
    TextView tv_sort;
    @BindView(R.id.tv_change)
    TextView tv_change;

    @BindView(R.id.iv_curreney)
    ImageView iv_curreney;
    @BindView(R.id.tv_person)
    TextView tv_person;
    @BindView(R.id.tv_currrncyname)
    TextView tv_currrncyname;
    @BindView(R.id.tv_group)
    TextView tv_group;

    @BindView(R.id.iv_account)
    ImageView iv_account;

    @BindView(R.id.tv_add1)
    TextView tv_add1;
    @BindView(R.id.tv_add2)
    TextView tv_add2;
    @BindView(R.id.ll_add)
    LinearLayout ll_add;
    @BindView(R.id.ll_search)
    LinearLayout ll_search;
    @BindView(R.id.rl_out)
    RelativeLayout rl_out;

    @BindView(R.id.ll_operation)
    LinearLayout ll_operation;//移动和删除布局


    @BindView(R.id.checkbox)
    CheckBox checkbox;


    private boolean isOperation = false;//移动和删除布局是否显示
    private boolean isAdd = false;//添加矿工布局是否显示
    private boolean isSearch = false;//搜索布局是否显示


    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private List<MinerBean.DataBean.MinerListBean> dataBeanList;
    private MinerListAdapter minerListAdapter;//矿工列表

    private String key = "";// 关键字 “”
    private int pagenum = 1;//页数
    private MinerSelectPop minerSelectPop;

    private CurrencyPopupWindow currencyPopupWindow;//币种选择
    private int currencyId;//币种id
    private List<CurrencyList.DataBean> currencyListData;


    private OnSelectPersonListener onSelectPersonListener;// 子账户选择
    private int childaccountId;//子账户id

    private LoadingDialog loadingDialog;

    private int State = -1;//状态 1在线 0离线 -1全部 -2失效
    private int Sort = 1;//排序 鱼池从上到下是 1 2 3 4 5 6

    private int MinerGroupID = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_miner, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();

        initMinerTypeSelectPop();

        currencyId = onSelectPersonListener.getCurrencyId();
        getCurrrencyData();


        childaccountId = onSelectPersonListener.childAccountId();
        if (onSelectPersonListener.getIsFastLogin()) {
            iv_account.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.daohang2));
            tv_person.setText(onSelectPersonListener.childAccountName() + " （只读页面）");
            checkbox.setVisibility(View.GONE);
            minerListAdapter.setIsFastLogin(true);

        } else {
            iv_account.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.daohang));
            tv_person.setText(onSelectPersonListener.childAccountName());
            checkbox.setVisibility(View.VISIBLE);
            minerListAdapter.setIsFastLogin(false);

        }


    }


    /**
     * 获取币种数据
     */
    private void getCurrrencyData() {
        currencyListData = new ArrayList<>();
        if (NetWorkUtil.check(getActivity())) {
            loadingDialog.show();

            NetHelperNew.GetCurrencyList(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "币种列表：" + data);
                    CurrencyList currencyList = MyGson.getInstance().fromJson(data, CurrencyList.class);
                    if (currencyList.getType() == 1) {

                        currencyListData.addAll(currencyList.getData());
                        initCurrencyPopupWindow();
                        if (currencyList.getData().size() > 0) {
                            if (currencyId == 0) {
                                GlideUtil.loadImage(getActivity(), NetHelperNew.URL + currencyList.getData().get(0).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                                tv_currrncyname.setText(currencyList.getData().get(0).getCode().toUpperCase());
                                currencyId = currencyList.getData().get(0).getId();
                                onSelectPersonListener.selectCurrency(currencyId);
                            } else {

                                for (int i = 0; i < currencyList.getData().size(); i++) {
                                    if (currencyId == currencyList.getData().get(i).getId()) {
                                        GlideUtil.loadImage(getActivity(), NetHelperNew.URL + currencyList.getData().get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                                        tv_currrncyname.setText(currencyList.getData().get(i).getCode().toUpperCase());
                                    }
                                }
                            }
                            //获取矿工数据
                            refreshClear();
                        }

                    } else {
                        new ErrorDialog(getContext(), currencyList.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(getActivity(), msg);
                }
            });


        }

    }

    /**
     * 币种选择pop初始化
     */
    private void initCurrencyPopupWindow() {
        currencyPopupWindow = new CurrencyPopupWindow(currencyListData, getActivity(), new CurrencyPopupWindow.OnSeleCurrencyLayoutListener() {
            @Override
            public void selectHouseLayout(int i) {
                currencyId = currencyListData.get(i).getId();
                GlideUtil.loadImage(getActivity(), NetHelperNew.URL + currencyListData.get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                tv_currrncyname.setText(currencyListData.get(i).getCode().toUpperCase());
                onSelectPersonListener.selectCurrency(currencyId);

            }
        });
    }


    // 当FRagmen被加载到activity的时候会被回调
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof OnSelectPersonListener) {
            onSelectPersonListener = (OnSelectPersonListener) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }

    //把传递进来的activity对象释放掉
    @Override
    public void onDetach() {
        super.onDetach();
        onSelectPersonListener = null;
    }


    /**
     * 矿工类型选择pop
     */
    private void initMinerTypeSelectPop() {
        minerSelectPop = new MinerSelectPop(getActivity(), new MinerSelectPop.OnSelectHouseLayoutListener() {
            @Override
            public void selectHouseLayout(int i) {
                Log.i("-->", "选择矿工类型：" + i);
                Sort = i + 1;
                refreshClear();

            }

            @Override
            public void selectName(String name) {
                tv_sort.setText(name);
            }
        });
    }

    /**
     * 初始化
     */
    private void initview() {

        EventBus.getDefault().register(this);
        loadingDialog = new LoadingDialog(getActivity(), 0);
        radioButton1.setChecked(true);
        radioButton1.setTextColor(getResources().getColor(R.color.white));
        radioButton2.setTextColor(getResources().getColor(R.color.black3));
        radioButton3.setTextColor(getResources().getColor(R.color.black3));
        radioButton4.setTextColor(getResources().getColor(R.color.black3));
        dataBeanList = new ArrayList<>();
        minerListAdapter = new MinerListAdapter(getActivity(), dataBeanList, new MinerListAdapter.OnCheckBoxListener() {
            @Override
            public void clickCheck() {
                //全选按钮取消
                checkbox.setChecked(false);


                //是否显示移动和删除布局
                Map<Integer, Boolean> isCheck = minerListAdapter.getIsCheck();
                boolean s = false;
                for (int i = 0; i < dataBeanList.size(); i++) {
                    if (isCheck.get(i)) {
                        s = true;
                        if (!isOperation) {
                            isOperation = true;
                            ll_operation.setVisibility(View.VISIBLE);
                            ll_operation.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomshow));
                        }

                    }
                }
                if (!s) {
                    if (isOperation) {
                        isOperation = false;
                        ll_operation.setVisibility(View.GONE);
                        ll_operation.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomhidden));
                    }

                }

            }
        });

        lRecyclerViewAdapter = new LRecyclerViewAdapter(minerListAdapter);
        //空白页
        mRecyclerView.setEmptyView(getView().findViewById(R.id.empty));
        getView().findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshClear();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(lRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader); //设置下拉刷新Progress的样式
        // mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);  //设置下拉刷新箭头
        //设置头部加载颜色
        mRecyclerView.setHeaderViewColor(R.color.system_color, R.color.system_color, android.R.color.white);
//设置底部加载颜色
        mRecyclerView.setFooterViewColor(R.color.system_color, R.color.system_color, android.R.color.white);

        //分割线
        DividerDecoration divider = new DividerDecoration.Builder(getActivity())
                .setHeight(R.dimen.d_1)
                .setPadding(R.dimen.d_0)
                .setColorResource(R.color.colorLine)
                .build();
        mRecyclerView.addItemDecoration(divider);
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                pagenum = 1;
                dataBeanList.clear();
                minerListAdapter.notifyDataSetChanged();
                getData(pagenum, false);
            }
        });
        //停止下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);

        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {//加载更多
            @Override
            public void onLoadMore() {
                getData(pagenum, false);
            }
        });


        //搜索框监听
        edit_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {

                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                    }
                    key = edit_content.getText().toString();

                    refreshClear();
                    return true;
                }
                return false;
            }
        });


        //全选
        checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkbox.isChecked()) {
                    minerListAdapter.initCheck(true);
                    minerListAdapter.notifyDataSetChanged();
                    if (!isOperation) {
                        isOperation = true;
                        ll_operation.setVisibility(View.VISIBLE);
                        ll_operation.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomshow));
                    }
                } else {
                    minerListAdapter.initCheck(false);
                    minerListAdapter.notifyDataSetChanged();

                    if (isOperation) {
                        isOperation = false;
                        ll_operation.setVisibility(View.GONE);
                        ll_operation.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomhidden));
                    }
                }
            }
        });

    }


    private boolean isRefresh = false;//是否正在请求

    /**
     * 请求矿工数据
     *
     * @param pageIndex
     */
    private void getData(int pageIndex, final boolean IsLoading) {

        if (childaccountId == 0) {
            radioButton1.setText("全部\n" + 0);
            radioButton2.setText("在线\n" + 0);
            radioButton3.setText("离线\n" + 0);
            radioButton4.setText("失效\n" + 0);
            return;
        }

        if (NetWorkUtil.check(getActivity()) && !isRefresh) {

            isRefresh = true;
            if (IsLoading)
                loadingDialog.show();
            NetHelperNew.GetOwnMinersList(pageIndex, currencyId + "", childaccountId + "", edit_content.getText().toString().trim(), State + "", Sort + "", MinerGroupID + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    isRefresh = false;
                    if (IsLoading)
                        loadingDialog.dismiss();

                    Log.i("-->", "矿工列表：" + data);
                    MinerBean minerBean = MyGson.getInstance().fromJson(data, MinerBean.class);


                    if (minerBean.getType() == 1) {


                        int onNumber = minerBean.getData().getOnNumber();//在线
                        int offNumber = minerBean.getData().getOffNumber();//离线
                        int loseNumber = minerBean.getData().getLoseNumber();//失效
                        int allNumber = onNumber + offNumber + loseNumber;//全部
                        radioButton1.setText("全部\n" + allNumber);
                        radioButton2.setText("在线\n" + onNumber);
                        radioButton3.setText("离线\n" + offNumber);
                        radioButton4.setText("失效\n" + loseNumber);
                        mRecyclerView.refreshComplete(10);

                        //更新数据
                        dataBeanList.addAll(minerBean.getData().getMinerList());

                        if (pagenum == 1) {
                            minerListAdapter.refrashMinerNotifyDataSetChanged();
                        } else {
                            minerListAdapter.moreMinerNotifyDataSetChanged();
                        }

                        minerListAdapter.setChildaccountId(childaccountId);
                        minerListAdapter.setCurrencyID(currencyId);

                        if (minerBean.getData().getMinerList().size() == 0) {

                            if (pagenum > 1) {
                                ToastUtil.showToast(getActivity(), "没有更多数据");
                            }

                        }
                        if (pagenum == 1) {
                            //全选按钮取消
                            checkbox.setChecked(false);
                            if (isOperation) {
                                isOperation = false;
                                ll_operation.setVisibility(View.GONE);
                                ll_operation.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.popbottomhidden));

                            }
                        }

                        pagenum++;
                    } else {
                        mRecyclerView.refreshComplete(10);
                        new ErrorDialog(getActivity(), minerBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    isRefresh = false;
                    if (IsLoading)
                        loadingDialog.dismiss();

                    mRecyclerView.refreshComplete(10);
                    ToastUtil.showToast(getActivity(), msg);
                }
            });
        }

    }

    @OnClick({R.id.ll_menu, R.id.ll_move, R.id.ll_del, R.id.iv_search2, R.id.iv_search, R.id.radioButton1, R.id.radioButton2, R.id.radioButton3, R.id.radioButton4, R.id.ll_minertype, R.id.ll_minergroup, R.id.ll_currency, R.id.tv_add1, R.id.tv_add2, R.id.fab01Add})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.ll_menu:
                //弹出抽屉
                onSelectPersonListener.selectPersonLayout(2, childaccountId);
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_add1:
                //添加矿机的方法
                AddMinerRemarkActivity.start(getActivity(), currencyId + "", tv_currrncyname.getText().toString().trim());
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }
                break;
            case R.id.iv_search:
                //搜索
                /*隐藏软键盘*/
                InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager.isActive() && getActivity().getCurrentFocus() != null) {
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
                }
                key = edit_content.getText().toString();

                refreshClear();
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }
                break;
            case R.id.ll_minertype://矿工类型

                if (minerSelectPop != null) {
                    minerSelectPop.showAsDropDown(view_line, 0, 0);
                    if (isAdd) {
                        isAdd = false;
                        ll_add.setVisibility(View.GONE);
                    }
                }

                break;
            case R.id.ll_minergroup://矿工分组

                getOwnMinerGroup();


                break;
            case R.id.ll_move://移动矿工分组

                getMoveOwnMinerGroup();


                break;
            case R.id.ll_del://删除矿工

                final List<String> names = new ArrayList<>();
                for (int i = 0; i < dataBeanList.size(); i++) {
                    if (minerListAdapter.getIsCheck().get(i)) {
                        names.add(dataBeanList.get(i).getName());
                    }
                }

                new ContentDialog(getActivity(), "确定要删除" + names.size() + "个矿工吗?", "提示", new ContentDialog.OnSuccessListener() {
                    @Override
                    public void sure() {

                        delMiner(names);
                    }
                }).show();


                break;
            case R.id.tv_add2://挖矿教程
                if (!"".equals(NetHelperNew.Tutorial + tv_currrncyname.getText().toString().trim())) {
                    ProfitWebAc.start(getActivity(), NetHelperNew.Tutorial + tv_currrncyname.getText().toString().trim(), tv_currrncyname.getText().toString().trim() + "挖矿教程");
                    if (isAdd) {
                        isAdd = false;
                        ll_add.setVisibility(View.GONE);
                    }

                }

                break;
            case R.id.radioButton1:
                //全部

                radioButton1.setTextColor(getResources().getColor(R.color.white));
                radioButton2.setTextColor(getResources().getColor(R.color.black3));
                radioButton3.setTextColor(getResources().getColor(R.color.black3));
                radioButton4.setTextColor(getResources().getColor(R.color.black3));
                tv_change.setText("拒绝率");
                State = -1;
                minerListAdapter.setType(0);
                refreshClear();
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }

                break;
            case R.id.radioButton2:
                //在线

                radioButton2.setTextColor(getResources().getColor(R.color.white));
                radioButton1.setTextColor(getResources().getColor(R.color.black3));
                radioButton3.setTextColor(getResources().getColor(R.color.black3));
                radioButton4.setTextColor(getResources().getColor(R.color.black3));
                tv_change.setText("拒绝率");
                State = 1;
                minerListAdapter.setType(0);
                refreshClear();
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }

                break;
            case R.id.radioButton3:
                //离线
                tv_change.setText("最近\n提交时间");
                radioButton3.setTextColor(getResources().getColor(R.color.white));
                radioButton1.setTextColor(getResources().getColor(R.color.black3));
                radioButton2.setTextColor(getResources().getColor(R.color.black3));
                radioButton4.setTextColor(getResources().getColor(R.color.black3));

                State = 0;
                minerListAdapter.setType(1);
                refreshClear();
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }

                break;
            case R.id.radioButton4:
                //失效
                tv_change.setText("最近\n提交时间");
                radioButton4.setTextColor(getResources().getColor(R.color.white));
                radioButton1.setTextColor(getResources().getColor(R.color.black3));
                radioButton2.setTextColor(getResources().getColor(R.color.black3));
                radioButton3.setTextColor(getResources().getColor(R.color.black3));
                State = -2;
                minerListAdapter.setType(1);
                refreshClear();
                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }

                break;
            case R.id.ll_currency:
                //选择币种
                if (currencyPopupWindow != null)
                    currencyPopupWindow.showAsDropDown(ll_currency, 0, 0);

                if (isAdd) {
                    isAdd = false;
                    ll_add.setVisibility(View.GONE);
                }

                break;

            case R.id.fab01Add:
                //FloatingActionButton
                isAdd = !isAdd;
                ll_add.setVisibility(isAdd ? View.VISIBLE : View.GONE);
                if (isAdd) {
                    ll_add.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.add_bill_anim));
                } else {
                    ll_add.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.add_bill_anim2));
                }
                break;
            case R.id.iv_search2:

                isSearch = !isSearch;
                ll_search.setVisibility(isSearch ? View.VISIBLE : View.GONE);
//                if (isSearch) {
//                    ll_search.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.add_bill_anim));
//                } else {
//                    ll_search.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.add_bill_anim2));
//                }

                break;
        }
    }

    /**
     * 移动矿工分组
     */
    private void getMoveOwnMinerGroup() {


        if (isAdd) {
            isAdd = false;
            ll_add.setVisibility(View.GONE);
        }

        if (NetWorkUtil.check(getActivity())) {
            loadingDialog.show();
            NetHelperNew.OwnMinerGroup(currencyId + "", childaccountId + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    loadingDialog.dismiss();

                    Log.i("-->", "获得矿机分组列表：" + data);
                    MinerGroupBean minerBean = MyGson.getInstance().fromJson(data, MinerGroupBean.class);


                    if (minerBean.getType() == 1) {

                        List<MinerGroupBean.DataBean> minergroupbeanlist = new ArrayList<>();
                        for (int i = 0; i < minerBean.getData().size(); i++) {
                            if (!"未分组".equals(minerBean.getData().get(i).getName())) {
                                minergroupbeanlist.add(minerBean.getData().get(i));
                            }
                        }
                        MoveMinerGroupPop minerGroupPop = new MoveMinerGroupPop(tv_person.getText().toString().trim(), currencyId + "", childaccountId + "", minergroupbeanlist, getActivity(), new MoveMinerGroupPop.OnSelectHouseLayoutListener() {
                            @Override
                            public void selectHouseLayout(String name, final int id) {
//                                MinerGroupID = i;
//                                refreshClear();
                                final List<String> names = new ArrayList<>();
                                for (int i = 0; i < dataBeanList.size(); i++) {
                                    if (minerListAdapter.getIsCheck().get(i)) {
                                        names.add(dataBeanList.get(i).getName());
                                    }
                                }

                                new ContentDialog(getActivity(), "将" + names.size() + "个矿工移动到 " + name + " ?", "提示", new ContentDialog.OnSuccessListener() {
                                    @Override
                                    public void sure() {
                                        String s = MyGson.getInstance().toJson(names);

                                        moveMinerGroup(id, names);
                                    }
                                }).show();
                            }


                        });

                        int[] location = new int[2];
                        ll_operation.getLocationOnScreen(location);
                        float hei = 40 * minergroupbeanlist.size() + 0.5f * (minerBean.getData().size() - 1)+10;
                        int he = DensityUtil.dip2px(getActivity(), hei);
                        Log.i("-->", location[1] + "pop高" + (location[1] - minerGroupPop.getHeight()) + "度" + he);
                        minerGroupPop.showAtLocation(ll_operation, Gravity.NO_GRAVITY, location[0], location[1] - he);
//                        minerGroupPop.showAsDropDown(ll_operation,0,-(location[1],gr));
//                        minerGroupPop.showAtLocation(ll_operation, Gravity.NO_GRAVITY,0, 0);

                    } else {

                        new ErrorDialog(getActivity(), minerBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();


                    ToastUtil.showToast(getActivity(), msg);
                }
            });
        }

    }

    /**
     * 删除矿工
     *
     * @param MinerNameList
     */
    private void delMiner(List<String> MinerNameList) {
        if (NetWorkUtil.check(getActivity())) {
            loadingDialog.show();
            NetHelperNew.DeleteMiners(MinerNameList, currencyId + "", childaccountId + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    loadingDialog.dismiss();

                    Log.i("-->", "删除矿机：" + data);
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        refreshClear();
                        ToastUtil.showToast(getActivity(), baseBean.getContent());
                    } else {

                        new ErrorDialog(getActivity(), baseBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();


                    ToastUtil.showToast(getActivity(), msg);
                }
            });
        }

    }

    /**
     * 移动矿工到分组
     *
     * @param MinerGroupID
     * @param MinerNameList
     */
    private void moveMinerGroup(int MinerGroupID, List<String> MinerNameList) {
        if (NetWorkUtil.check(getActivity())) {
            loadingDialog.show();
            NetHelperNew.MoveMinerGroup(MinerGroupID + "", MinerNameList, currencyId + "", childaccountId + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    loadingDialog.dismiss();

                    Log.i("-->", "移动矿机到分组：" + data);
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        ToastUtil.showToast(getActivity(), baseBean.getContent());
                    } else {

                        new ErrorDialog(getActivity(), baseBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();


                    ToastUtil.showToast(getActivity(), msg);
                }
            });
        }

    }

    /**
     * 获得矿机分组列表
     */
    private void getOwnMinerGroup() {


        if (isAdd) {
            isAdd = false;
            ll_add.setVisibility(View.GONE);
        }

        if (NetWorkUtil.check(getActivity())) {
            loadingDialog.show();
            NetHelperNew.OwnMinerGroup(currencyId + "", childaccountId + "", new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {


                    loadingDialog.dismiss();

                    Log.i("-->", "获得矿机分组列表：" + data);
                    MinerGroupBean minerBean = MyGson.getInstance().fromJson(data, MinerGroupBean.class);


                    if (minerBean.getType() == 1) {

                        List<MinerGroupBean.DataBean> data1 = new ArrayList<>();
                        MinerGroupBean.DataBean dataBeanss = new MinerGroupBean.DataBean();
                        dataBeanss.setID(-1);
                        dataBeanss.setName("全部");
                        double Hashrate = 0.0;
                        int OnNumber = 0;
                        int OffNumber = 0;
                        int LoseNumber = 0;
                        for (int i = 0; i < minerBean.getData().size(); i++) {
                            MinerGroupBean.DataBean dataBean = minerBean.getData().get(i);
                            OnNumber = OnNumber + dataBean.getOnNumber();
                            OffNumber = OffNumber + dataBean.getOffNumber();
                            LoseNumber = LoseNumber + dataBean.getLoseNumber();
                            Hashrate = Hashrate + Double.valueOf(dataBean.getHashrate());
                        }
                        dataBeanss.setHashrate("" + Hashrate);
                        dataBeanss.setOnNumber(OnNumber);
                        dataBeanss.setOffNumber(OffNumber);
                        dataBeanss.setLoseNumber(LoseNumber);
                        data1.add(dataBeanss);
                        data1.addAll(minerBean.getData());
                        MinerGroupPop minerGroupPop = new MinerGroupPop(MinerGroupID,onSelectPersonListener.getIsFastLogin(),tv_person.getText().toString().trim(), currencyId + "", childaccountId + "", data1, getActivity(), new MinerGroupPop.OnSelectHouseLayoutListener() {
                            @Override
                            public void selectHouseLayout(int i) {
                                MinerGroupID = i;
                                refreshClear();
                            }

                            @Override
                            public void selectName(String name) {
                                tv_group.setText(name + "");

                            }
                        });
                        minerGroupPop.showAsDropDown(view_line, 0, 0);

                    } else {

                        new ErrorDialog(getActivity(), minerBean.getContent()).show();
                    }


                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();


                    ToastUtil.showToast(getActivity(), msg);
                }
            });
        }

    }


    /**
     * 清空刷新数据
     */
    private void refreshClear() {
        dataBeanList.clear();
        minerListAdapter.notifyDataSetChanged();
        pagenum = 1;
        getData(pagenum, true);
    }



    @Subscribe(threadMode = ThreadMode.MAIN, sticky = false, priority = 0)
    public void handleData(String str) {

        if (str.equals("222")) {
            if (dataBeanList != null && isUserVisibleRefresh) {
                isUserVisibleRefresh = false;
                refreshClear();

                Log.i("-->", "====miner============" + str);
            }
        }
        rl_out.requestLayout();

    }

    private int fastloginNum = 0;//0正常登录 1快速
    private boolean isUserVisibleRefresh = false;//可见是否刷新

    @Override
    public void updatePerson(int type, String name, int id) {

        tv_group.setText("全部");
        MinerGroupID = -1;

        childaccountId = id;


        if (type == 3) {
            //快速登录
            if (fastloginNum == 0) {
                fastloginNum = 1;
                isUserVisibleRefresh = true;
            }
            tv_person.setText(name + " （只读页面）");
            iv_account.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.daohang2));
            checkbox.setVisibility(View.GONE);
            minerListAdapter.setIsFastLogin(true);

        } else {
            if (fastloginNum == 1) {
                fastloginNum = 0;
                isUserVisibleRefresh = true;
            }
            tv_person.setText(name);

            iv_account.setImageDrawable(getActivity().getResources().getDrawable(R.mipmap.daohang));
            checkbox.setVisibility(View.VISIBLE);
            minerListAdapter.setIsFastLogin(false);
        }
        refreshClear();
        Log.i("-->", type + "MInerfragment" + name);
    }

    /**
     * 修改币种
     *
     * @param img
     * @param name
     * @param id
     */
    @Override
    public void updateCurrency(int img, String name, int id) {
        tv_group.setText("全部");
        MinerGroupID = -1;

        currencyId = id;
        for (int i = 0; i < currencyListData.size(); i++) {
            if (currencyListData.get(i).getId() == id) {
                GlideUtil.loadImage(getActivity(), NetHelperNew.URL + currencyListData.get(i).getIcon(), iv_curreney, R.mipmap.failimage, R.mipmap.failimage);
                tv_currrncyname.setText(currencyListData.get(i).getCode().toUpperCase());
            }
        }
        refreshClear();

    }

    @Override
    public void fragmentRefresh(int type) {

    }
}
